package com.bbcare.cp.plat.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ToolUtil;
import com.bbcare.cp.plat.entity.au.AuProject;
import com.bbcare.cp.plat.entity.au.AuProjectRoleRel;
import com.bbcare.cp.plat.entity.cp.FollowQueue;
import com.bbcare.cp.plat.entity.cp.FuQueueCpRel;
import com.bbcare.cp.plat.service.IProjectQueueCpService;
import com.bbcare.cp.plat.store.ibatis.au.IAuProjectDAO;
import com.bbcare.cp.plat.store.ibatis.cp.IFollowQueueDAO;
import com.bbcare.cp.plat.store.ibatis.cp.IFuQueueCpRelDAO;
import com.bbcare.cp.plat.store.ibatis.dictconf.IConfCacheDAO;

/**
 * @name: 项目--队列--临床路径接口实现（通用接口独立出来）
 * @author: DemonLee
 * @createTime: 2018.4.25
 * @description: 项目--队列--临床路径等相关的操作
 * @modify:
 *
 */
@Service
public class ProjectQueueCpServiceImpl implements IProjectQueueCpService {
	private final Log logger = LogFactory
			.getLog(ProjectQueueCpServiceImpl.class);

	@Autowired
	private IFollowQueueDAO queueDao;

	@Autowired
	private IFuQueueCpRelDAO fuQueueCpRelDao;

	@Autowired
	public IConfCacheDAO confCacheDao;

	@Autowired
	public IAuProjectDAO auProjectDao;

	/**
	 * @name: 参数是否允许为空校验接口实现
	 * @author: DemonLee
	 * @createTime: 2018.4.25
	 * @param: 问题id、临床路径id或队列id等
	 * @return: map
	 * @modify:
	 */
	@Override
	public boolean isVerifyArchiveParam(Map<String, Object> mapIn)
			throws Exception {
		// paramCode可以是队列id，也可以是方案id等
		String paramCode = (String) mapIn.get("paramCode");
		// paramValue为参数id
		String paramValue = (String) mapIn.get("paramValue");
		if (StringUtils.isBlank(paramCode) || StringUtils.isBlank(paramValue)) {
			return true;
		}

		Map<String, Object> qryMapIn = new HashMap<String, Object>();
		qryMapIn.put("paramValue", paramValue);
		qryMapIn.put("type", Constants.QUEUE_ARCHIVE_TASKCONT_NONVERIFY_TYPE);
		qryMapIn.put("paramCode", paramCode);
		List<Map<String, Object>> retList = confCacheDao
				.selectBsStaticParamByTenant(qryMapIn);
		if (CollectionUtils.isNotEmpty(retList)) {
			return false;
		}

		return true;
	}

	/**
	 * @name: 队列--路径查询及校验接口实现
	 * @author: DemonLee
	 * @createTime: 2018.4.25
	 * @param: 队列id、临床路径id
	 * @return: map
	 * @modify:
	 */
	@Override
	public Map<String, Object> qryQueueCpService(Map<String, Object> argMap)
			throws Exception {
		logger.debug("---querySchemeArchive---argMap==" + argMap);
		Map<String, Object> outMap = new HashMap<String, Object>();
		Map<String, Object> mapIn = new HashMap<String, Object>(argMap);
		String schemeId = (String) mapIn.get("schemeId");
		String queueId = (String) mapIn.get("queueId");

		// step1: 队列信息校验
		Map<String, Object> qryMap = new HashMap<String, Object>();
		qryMap.put("id", queueId);
		List<FollowQueue> queueList = queueDao.selectById(qryMap);
		if (CollectionUtils.isEmpty(queueList)) {
			throw (new AppException(
					ErrorCode.FOLLOWUP_EXCEPTION_TYPE_QUEUE_NOT_EXIST,
					"队列信息不存在，请核实配置！"));
		}
		FollowQueue fq = (FollowQueue) queueList.get(0);
		if (Constants.QUEUE_STATUS_DELETE.equals(fq.getQueueStatus())) {
			throw (new AppException(
					ErrorCode.FOLLOWUP_EXCEPTION_TYPE_QUEUE_STATUS_IRREGULAR,
					"队列状态不正常，无法处理！"));
		}

		// step2: 校验队列-路径关联信息
		qryMap.clear();
		qryMap.put("queueId", queueId);
		qryMap.put("cpId", schemeId);
		List<FuQueueCpRel> fqcrList = fuQueueCpRelDao
				.listQueueCpByCondition(qryMap);
		if (CollectionUtils.isEmpty(fqcrList)) {
			throw (new AppException(
					ErrorCode.CPQUEUE_EXCEPTION_QUEUE_CP_REL_NOT_EXISTS,
					"该队列下未关联对应临床路径方案，请核实配置信息！"));
		}

		// step3: 返回队列及队列--路径关联信息
		outMap.put("FollowQueue", fq);
		outMap.put("FuQueueCpRel", fqcrList.get(0));

		return outMap;
	}

	/**
     *
     */
	/**
	 * 根据projectId 去查询相关项目信息
	 *
	 * @author fangxm
	 * @date 2018年5月4日
	 * @param
	 * @return map
	 * @description:
	 */
	@Override
	public Map<String, Object> qryProjectQueue(Map<String, Object> argMap)
			throws Exception {
		logger.debug("---qryProjectQueue---argMap==" + argMap);
		Map<String, Object> outMap = new HashMap<String, Object>();
		Map<String, Object> mapIn = new HashMap<String, Object>(argMap);
		String projectId = (String) mapIn.get("projectId");
		String projectRoleId = (String) mapIn.get("projectRoleId");
		String tenantId = (String) mapIn.get("tenantId");
		int queueTotal = 0;
		// step1: 查询项目
		Map<String, Object> qryMap = new HashMap<String, Object>();
		qryMap.put("projectId", projectId);
		List<AuProject> projectList = auProjectDao.selectProjectById(qryMap);
		if (CollectionUtils.isEmpty(projectList)) {
			throw (new AppException(
					ErrorCode.WECHAT_EXCEPTION_PROJECT_NOT_CONFIG, "项目不存在，请核实！"));
		}
		// step2: 查询项目下关联的队列
		qryMap.put("projectRoleId", projectRoleId);
        qryMap.put("tenantId", tenantId);
        qryMap.put("remark", (String) mapIn.get("subBusiType"));
		qryMap.put("status", Constants.TABLE_RECORD_STATUS_NOMAL);// 队列关联有效
		qryMap.put("queueStatus", Constants.QUEUE_STATUS_NOMAL);// 队列状态
		qryMap.put("queueUserType", argMap.get("queueUserType"));

		List<FollowQueue> followQueueList = queueDao.listQueueByRelPro(qryMap);
		List<String> queueIdIn = new ArrayList<String>();
		if (CollectionUtils.isNotEmpty(followQueueList)) {
			queueTotal = followQueueList.size();
            for (FollowQueue followQueue : followQueueList) {
                if (StringUtils.isNotBlank(followQueue.getId())) {
                    queueIdIn.add(followQueue.getId());// 将队列放入List 作为条件
                }
            }
		}
		// step3: 返回相关信息
		outMap.put("projectInfo", projectList.get(0));
		outMap.put("queueList", followQueueList);
		outMap.put("queueTotal", queueTotal);
		outMap.put("queueIdIn", queueIdIn);
		return outMap;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.bbcare.cp.plat.service.IProjectQueueCpService#qryProjectRoleByQueueId
	 * (java.util.Map)
	 */
	@Override
	public List<AuProjectRoleRel> qryProjectRoleByQueueId(
			Map<String, Object> argMap) throws Exception {
		List<AuProjectRoleRel> roleList = new ArrayList<AuProjectRoleRel>();

		String queueId = ToolUtil.getStringFromMap(argMap, "queueId");
		Map<String, Object> qryMap = new HashMap<String, Object>();
		qryMap.put("queueId", queueId);
		qryMap.put("status", Constants.COMMON_STATUS_NOMAL);
		List<AuProject> projectList = auProjectDao.selectProjectByQueueId(qryMap);
		if (!CollectionUtils.isEmpty(projectList)) {
			String projectId = projectList.get(0).getId();
			// 查询项目下所有角色id
			qryMap.clear();
			qryMap.put("projectId", projectId);
			qryMap.put("status", Constants.COMMON_STATUS_NOMAL);
			roleList = auProjectDao.selectPatientRoleByProject(qryMap);
		}
		return roleList;
	}

	/* 根据roleType 去查询角色 */
	@Override
	public String qryProjectRoleByRoleType(Map<String, Object> argMap)
			throws Exception {
		List<AuProjectRoleRel> roleList = new ArrayList<AuProjectRoleRel>();
		Map<String, Object> qryMap = new HashMap<String, Object>();
		qryMap.put("roleType", argMap.get("roleType"));
		qryMap.put("projectId", argMap.get("projectId"));
		qryMap.put("tenantId", argMap.get("tenantId"));
		roleList = auProjectDao.selectPatientRoleByProject(qryMap);
		String projectRoleId = "";
		if (CollectionUtils.isNotEmpty(roleList)) {
			projectRoleId = roleList.get(0).getId();
		}
		// 患者端过来的只有1个 角色
		return projectRoleId;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.bbcare.cp.plat.service.IProjectQueueCpService#qryProjectByQueueId
	 * (java.util.Map)
	 */
	@Override
	public AuProject qryProjectByQueueId(Map<String, Object> argMap)
			throws Exception {
		String queueId = ToolUtil.getStringFromMap(argMap, "queueId");
		Map<String, Object> qryMap = new HashMap<String, Object>();
		qryMap.put("queueId", queueId);
		qryMap.put("status", Constants.COMMON_STATUS_NOMAL);
		List<AuProject> projectList = auProjectDao.selectProjectByQueueId(qryMap);
		if (!CollectionUtils.isEmpty(projectList)) {
			return projectList.get(0);
		}else{
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.bbcare.cp.plat.service.IProjectQueueCpService#qryQueueByQueueId(java.util.Map)
	 */
	@Override
	public FollowQueue qryQueueByQueueId(Map<String, Object> argMap)
			throws Exception {
		 // step2: 队列基本信息校验
		String queueId = MapUtils.getString(argMap,"queueId");
        Map<String, Object> qryMap = new HashMap<String, Object>();
        qryMap.put("id", queueId);
        List<FollowQueue> queueList = queueDao.selectById(qryMap);
        if (CollectionUtils.isEmpty(queueList)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_QUEUE_NOT_EXIST, "队列信息不存在，请核实配置！"));
        }
        FollowQueue fq = (FollowQueue) queueList.get(0);
        return fq;
	}

}
